import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:tot_android_flutter/utils/utils_ext.dart';

class TabLayout extends StatefulWidget {
  /**
   * 构造一个自定义的TabBar和TabBarView组件。
   */
  final List<Widget> tabs; // 表示Tab栏上每个Tab的Widget列表。
  final List<Widget> tabViews; // 表示每个Tab对应的视图Widget列表。
  final TabController controller; // 控制Tab的选中状态和动画的TabController。
  final Color selectedColor; // 选中状态时Tab的文字颜色。
  final Color unselectedColor; // 未选中状态时Tab的文字颜色。
  final Color indicatorColor; // Tab选中时下划线的颜色。
  final bool isScrollable; // 是否允许Tab栏滚动。
  final double indicatorWeight; // 下划线的宽度。
  final EdgeInsetsGeometry labelPadding; // Tab标签的内边距。
  final EdgeInsetsGeometry tabPadding; // Tab本身的内边距。
  final Color backgroundColor; // Tab栏的背景颜色。
  final bool removeAppBarShadow; // 是否移除AppBar的阴影。
  final EdgeInsetsGeometry indicatorPadding; // 指示器边距

  TabLayout({
    required this.tabs,
    required this.tabViews,
    required this.controller,
    this.selectedColor = Colors.blue,
    this.unselectedColor = Colors.grey,
    this.indicatorColor = Colors.blue,
    this.isScrollable = true,
    this.indicatorWeight = 4.0,
    this.labelPadding = const EdgeInsets.symmetric(horizontal: 16.0),
    this.tabPadding = const EdgeInsets.symmetric(vertical: 8),
    this.backgroundColor = Colors.transparent,
    this.removeAppBarShadow = true,
    this.indicatorPadding = const EdgeInsets.all(0),
  });

  @override
  _TabLayoutState createState() => _TabLayoutState();
}

class _TabLayoutState extends State<TabLayout> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        elevation: widget.removeAppBarShadow ? 0 : 4,
        backgroundColor: widget.backgroundColor,
        toolbarHeight: 0,
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(48.0),
          child: Container(
              decoration: BoxDecoration(
                color: widget.backgroundColor,
                border: const Border(
                  bottom: BorderSide(
                    color: Colors.grey, // divider 的颜色
                    width: 1.0, // divider 的宽度
                  ),
                ),
              ),
              child: Container(
                color: Colors.pink,
                child: TabBar(
                  controller: widget.controller,
                  tabs: widget.tabs,
                  isScrollable: widget.isScrollable,
                  indicator: UnderlineTabIndicator(
                    borderSide: BorderSide(
                      color: widget.indicatorColor,
                      width: widget.indicatorWeight,
                    ),
                    insets: EdgeInsets.only(bottom: -8), // 设置下划线距离标签的距离
                  ),
                  labelColor: widget.selectedColor,
                  unselectedLabelColor: widget.unselectedColor,
                  labelPadding: widget.labelPadding,
                  indicatorPadding: widget.indicatorPadding,
                ),
              )),
        ),
      ),
      body: Padding(
        padding: widget.tabPadding,
        child: TabBarView(
          controller: widget.controller,
          children: widget.tabViews,
        ),
      ),
    );
  }
}
